//
//  RBCompeDetailTabHeaderView.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/13.
//

#import "RBCompeDetailTabHeaderView.h"
#import "RBCompeGameDetailModel.h"

@interface RBCompeDetailTabHeaderView ()
@property(strong,nonatomic)UIView *bgView;
@property(strong,nonatomic)UIImageView *bgImageView;
@property(strong,nonatomic)UIView *navBgView;
@property(strong,nonatomic)UILabel *titleLabel;
@property(strong,nonatomic)UILabel *timeLabel;
@property(strong,nonatomic)UIButton *collectButton;
@property(strong,nonatomic)UILabel *typeLabel;
@property(strong,nonatomic)UILabel *scoreVSLabel;
@property(strong,nonatomic)UILabel *statueLabel;
@property(strong,nonatomic)UILabel *homeTeamLabel;
@property(strong,nonatomic)UIImageView *homeTeamImageView;
@property(strong,nonatomic)UILabel *homeNameLabel;
@property(strong,nonatomic)UILabel *awayTeamLabel;
@property(strong,nonatomic)UIImageView *awayTeamImageView;
@property(strong,nonatomic)UILabel *awayNameLabel;
@end

@implementation RBCompeDetailTabHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self rb_initViewUI];
        [self setNeedsLayout];
    }
    return self;
}
-(void)rb_initViewUI{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.bgImageView];
    [self.bgView addSubview:self.navBgView];
    [self.navBgView addSubview:self.backButton];
    [self.navBgView addSubview:self.titleLabel];
    [self.navBgView addSubview:self.timeLabel];
    [self.navBgView addSubview:self.collectButton];
    [self.bgView addSubview:self.typeLabel];
    [self.bgView addSubview:self.scoreVSLabel];
    [self.bgView addSubview:self.statueLabel];
    [self.bgView addSubview:self.homeTeamLabel];
    [self.bgView addSubview:self.homeTeamImageView];
    [self.bgView addSubview:self.homeNameLabel];
    [self.bgView addSubview:self.awayTeamLabel];
    [self.bgView addSubview:self.awayTeamImageView];
    [self.bgView addSubview:self.awayNameLabel];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
    [self.navBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(RBNAVIGATION_BAR_HEIGHT);
    }];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.navBgView.mas_bottom).mas_equalTo(-RBScaleWidth(5));
        make.left.mas_equalTo(RBScaleWidth(10));
        make.width.height.mas_equalTo(RBScaleWidth(30));
    }];
    [self.collectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.centerY.mas_equalTo(self.backButton.mas_centerY);
        make.width.height.mas_equalTo(RBScaleWidth(30));
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-RBScaleWidth(5));
        make.height.mas_equalTo(RBScaleWidth(18));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.timeLabel.mas_top);
        make.height.mas_equalTo(RBScaleWidth(13));
        make.centerX.mas_equalTo(0);
    }];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).mas_offset(RBScaleWidth(34));
        make.height.mas_equalTo(RBScaleWidth(12));
    }];
    [self.scoreVSLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.typeLabel.mas_centerX);
        make.top.mas_equalTo(self.typeLabel.mas_bottom).mas_offset(RBScaleWidth(22));
        make.height.mas_equalTo(RBScaleWidth(22));
    }];
    [self.statueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.scoreVSLabel.mas_centerX);
        make.top.mas_equalTo(self.scoreVSLabel.mas_bottom).mas_offset(RBScaleWidth(22));
        make.height.mas_equalTo(RBScaleWidth(12));
    }];
    [self.homeTeamLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(RBScaleWidth(60));
        make.centerY.mas_equalTo(self.typeLabel.mas_centerY);
    }];
    [self.homeTeamImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(RBScaleWidth(48));
        make.centerX.mas_equalTo(self.homeTeamLabel.mas_centerX);
        make.centerY.mas_equalTo(self.scoreVSLabel.mas_centerY);
    }];
    self.homeTeamImageView.layer.cornerRadius = RBScaleWidth(24);
    self.homeTeamImageView.layer.masksToBounds = YES;
    self.homeTeamImageView.clipsToBounds = YES;
    [self.homeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.homeTeamLabel.mas_centerX);
        make.centerY.mas_equalTo(self.statueLabel.mas_centerY);
    }];
    [self.awayTeamLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RBScaleWidth(60));
        make.centerY.mas_equalTo(self.typeLabel.mas_centerY);
    }];
    [self.awayTeamImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(RBScaleWidth(48));
        make.centerX.mas_equalTo(self.awayTeamLabel.mas_centerX);
        make.centerY.mas_equalTo(self.scoreVSLabel.mas_centerY);
    }];
    self.awayTeamImageView.layer.cornerRadius = RBScaleWidth(24);
    self.awayTeamImageView.layer.masksToBounds = YES;
    self.awayTeamImageView.clipsToBounds = YES;
    [self.awayNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.awayTeamLabel.mas_centerX);
        make.centerY.mas_equalTo(self.statueLabel.mas_centerY);
    }];
}
- (void)setCompeGameDetailModel:(RBCompeGameDetailModel *)compeGameDetailModel{
    _compeGameDetailModel=compeGameDetailModel;
    if ([compeGameDetailModel.sportId isEqualToString:@"1"]) {//足球
        if (!RBStringIsEmpty(compeGameDetailModel.round.round_num)) {
            self.titleLabel.text = [NSString stringWithFormat:@"%@ | 第%@轮",compeGameDetailModel.competitionName,compeGameDetailModel.round.round_num];
        } else {
            self.titleLabel.text = [NSString stringWithFormat:@"%@",compeGameDetailModel.competitionName];
        }
    }else{
        self.titleLabel.text = [NSString stringWithFormat:@"%@",compeGameDetailModel.competitionName];
    }

    if (!RBStringIsEmpty(compeGameDetailModel.jcNum)) {
        NSString *weekStr = [compeGameDetailModel.jcNum substringToIndex:1];;
        weekStr = [NSString rb_NSStringReplaceWeekDay:weekStr];
        NSString *ciStr = [compeGameDetailModel.jcNum substringFromIndex:1];
        self.timeLabel.text = [NSString stringWithFormat:@"%@ %@ %@",compeGameDetailModel.matchTime,weekStr,ciStr];
    } else {
        if (!RBStringIsEmpty(compeGameDetailModel.bdNum)) {
            self.timeLabel.text = [NSString stringWithFormat:@"%@ 北单%@",compeGameDetailModel.matchTime,compeGameDetailModel.bdNum];
        }else{
            self.timeLabel.text = @"";
        }
    }
    if (RBStringIsEmpty(compeGameDetailModel.homeNameZh)) {
        compeGameDetailModel.homeNameZh = @"";
    }
    if (RBStringIsEmpty(compeGameDetailModel.awayNameZh)) {
        compeGameDetailModel.awayNameZh = @"";
    }
    if ([compeGameDetailModel.sportId isEqualToString:@"1"]) {//足球
        self.bgImageView.image = [UIImage imageNamed:@"football_bg"];
        self.homeTeamLabel.text = @"(主)";
        self.homeNameLabel.text = compeGameDetailModel.homeNameZh;
        [self.homeTeamImageView sd_setImageWithURL:[NSURL URLWithString:compeGameDetailModel.homeLogo] placeholderImage:[UIImage imageNamed:@""]];
        self.awayTeamLabel.text = @"(客)";
        self.awayNameLabel.text = compeGameDetailModel.awayNameZh;
        [self.awayTeamImageView sd_setImageWithURL:[NSURL URLWithString:compeGameDetailModel.awayLogo] placeholderImage:[UIImage imageNamed:@""]];
        NSString *homeScore = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:0];
        NSString *awayScore = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:0];
        self.scoreVSLabel.text = [NSString stringWithFormat:@"%@ : %@",homeScore ,awayScore];
        self.statueLabel.hidden = YES;
        switch ([compeGameDetailModel.statusId intValue]) {
            case 1:
                self.typeLabel.text = @" 未开赛 ";
                break;
            case 2:
                self.typeLabel.text = @" 上半场 ";
                self.typeLabel.text = @"进行中";
                break;
            case 3:
                self.typeLabel.text = @"中场";
                break;
            case 4:
            {
                self.typeLabel.text = @" 下半场 ";
                NSString *homeScore = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:0];
                NSString *awayScore = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:0];
                self.statueLabel.text = [NSString stringWithFormat:@"(半场%@:%@)",homeScore,awayScore];
                self.statueLabel.hidden = NO;
            }
                break;
            case 5:
                self.typeLabel.text = @" 加时赛 ";
                break;
            case 6:
                self.typeLabel.text = @" 加时赛(弃用) ";
                break;
            case 7:
                self.typeLabel.text = @" 点球决战 ";
                break;
            case 8:
                self.typeLabel.text = @" 完场 ";
                break;
            case 9:
                self.typeLabel.text = @" 推迟 ";
                break;
            case 10:
                self.typeLabel.text = @" 中断 ";
                break;
            case 11:
                self.typeLabel.text = @" 腰斩 ";
                break;
            case 12:
                self.typeLabel.text = @" 取消 ";
                break;
            case 13:
                self.typeLabel.text = @" 待定 ";
                break;
            default:
                self.typeLabel.text = @" 比赛异常 ";
                break;
        }
        
        if ([self.typeLabel.text containsString:@"完场"]||[self.typeLabel.text containsString:@"进行中"]) {
            NSString *homeScore = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:1];
            NSString *awayScore = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:1];
            self.statueLabel.text = [NSString stringWithFormat:@"半场%@:%@",homeScore,awayScore];
            self.statueLabel.hidden = NO;
            self.collectButton.hidden = YES;
            NSString *homeExtra = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:5];
            NSString *awayExtra = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:5];
            if ([homeExtra intValue] !=0 || [awayExtra intValue] !=0) {
                self.statueLabel.text = [NSString stringWithFormat:@"%@ 加时%@:%@",self.statueLabel.text,homeExtra,awayExtra];
            }
            NSString *homePenalty = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:6];
            NSString *awayPenalty = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:6];
            if ([homePenalty intValue] !=0 || [awayPenalty intValue] !=0) {
                self.statueLabel.text = [NSString stringWithFormat:@"%@ 点球%@:%@",self.statueLabel.text,homePenalty,awayPenalty];
            }
            self.statueLabel.text = [NSString stringWithFormat:@"(%@)",self.statueLabel.text];
        }else{
            self.statueLabel.hidden = YES;
        }
    } else {//篮球
        self.bgImageView.image = [UIImage imageNamed:@"basball_bg"];
        self.homeTeamLabel.text = @"(客)";
        self.homeNameLabel.text = compeGameDetailModel.awayNameZh;
        [self.homeTeamImageView sd_setImageWithURL:[NSURL URLWithString:compeGameDetailModel.awayLogo] placeholderImage:[UIImage imageNamed:@""]];
        self.awayTeamLabel.text = @"(主)";
        self.awayNameLabel.text = compeGameDetailModel.homeNameZh;
        [self.awayTeamImageView sd_setImageWithURL:[NSURL URLWithString:compeGameDetailModel.homeLogo] placeholderImage:[UIImage imageNamed:@""]];
        NSInteger homeScore = [[[compeGameDetailModel.homeScoresArray mj_JSONObject] valueForKeyPath:@"@sum.floatValue"] integerValue];
        NSInteger awayScore = [[[compeGameDetailModel.awayScoresArray mj_JSONObject] valueForKeyPath:@"@sum.floatValue"] integerValue];
        self.scoreVSLabel.text = [NSString stringWithFormat:@"%ld : %ld",awayScore ,homeScore];
        self.statueLabel.hidden = YES;
        switch ([compeGameDetailModel.statusId intValue]) {
            case 1:
                self.typeLabel.text = @" 未开赛 ";
                break;
            case 2:
                self.typeLabel.text = [NSString stringWithFormat:@"进行中  第一节"];
                break;
            case 3:
                self.typeLabel.text = @"中场";
                break;
            case 4:
            {
                self.typeLabel.text = [NSString stringWithFormat:@"进行中  第二节"];
                NSString *homeScore = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:0];
                NSString *awayScore = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:0];
                self.statueLabel.text = [NSString stringWithFormat:@"(第一节%@:%@)",homeScore,awayScore];
                self.statueLabel.hidden = NO;
            }
                break;
            case 5:
                self.typeLabel.text = @" 加时赛 ";
                break;
            case 6:
            {
                self.typeLabel.text = [NSString stringWithFormat:@"进行中  第三节"];
                NSString *homeScore = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:1];
                NSString *awayScore = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:1];
                self.statueLabel.text = [NSString stringWithFormat:@"(第二节%@:%@)",homeScore,awayScore];
                self.statueLabel.hidden = NO;
            }
                break;
            case 7:
                break;
            case 8:
            {
                self.typeLabel.text = [NSString stringWithFormat:@"进行中  第四节"];
                NSString *homeScore = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:2];
                NSString *awayScore = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:2];
                self.statueLabel.text = [NSString stringWithFormat:@"(第三节%@:%@)",homeScore,awayScore];
                self.statueLabel.hidden = NO;
            }
                break;
            case 9:
            {
                self.typeLabel.text = [NSString stringWithFormat:@"进行中  加时"];
                NSString *homeScore = [compeGameDetailModel.awayScoresArray rb_objectAtIndex:2];
                NSString *awayScore = [compeGameDetailModel.homeScoresArray rb_objectAtIndex:2];
                self.statueLabel.text = [NSString stringWithFormat:@"(第四节%@:%@)",homeScore,awayScore];
                self.statueLabel.hidden = NO;
            }
                break;
            case 10:
                self.typeLabel.text = @" 完场 ";
                break;
            case 11:
                self.typeLabel.text = @" 中断 ";
                break;
            case 12:
                self.typeLabel.text = @" 取消 ";
                break;
            case 13:
                self.typeLabel.text = @" 延期 ";
                break;
            case 14:
                self.typeLabel.text = @" 腰斩 ";
                break;
            case 15:
                self.typeLabel.text = @" 待定 ";
                break;
            default:
                self.typeLabel.text = @" 比赛异常 ";
                break;
        }
        if ([self.typeLabel.text containsString:@"完场"]||[self.typeLabel.text containsString:@"进行中"]) {
            self.collectButton.hidden = YES;
        }
    }
    if ([compeGameDetailModel.followStatus intValue] == 1) {
        [self.collectButton setImage:[UIImage imageNamed:@"yscc"] forState:UIControlStateNormal];
    }else{
        [self.collectButton setImage:[UIImage imageNamed:@"wsc_b"] forState:UIControlStateNormal];
    }
    if ([self.typeLabel.text containsString:@"未开赛"]) {
        self.scoreVSLabel.text = @"VS";
    }
}
-(void)backButtonTap{
    if (self.backButtonTapBlock) {
        self.backButtonTapBlock();
    }
}
-(void)collectButtonTap{
    if (self.collectButtonTapBlock) {
        self.collectButtonTapBlock(_compeGameDetailModel);
    }
}
- (UIView *)bgView{
    if(_bgView==nil){
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor clearColor];
    }
    return _bgView;
}
- (UIImageView *)bgImageView{
    if(_bgImageView==nil){
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageNamed:@"football_bg"];
        _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _bgImageView;
}
- (UIView *)navBgView{
    if(_navBgView==nil){
        _navBgView = [[UIView alloc] init];
        _navBgView.backgroundColor = [UIColor clearColor];
    }
    return _navBgView;
}
- (UIButton *)backButton{
    if(_backButton==nil){
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"backWhite"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(backButtonTap) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}
-(UILabel *)titleLabel{
    if(_titleLabel==nil){
        _titleLabel = [UILabel rb_labelWithTitle:@"日职" color:RBColor(255, 255, 255) font:RBPingFangFont(12) alignment:NSTextAlignmentCenter];
    }
    return _titleLabel;
}
-(UILabel *)timeLabel{
    if(_timeLabel==nil){
        _timeLabel = [UILabel rb_labelWithTitle:@"时间" color:RBColor(255, 255, 255) font:RBPingFangFont(10) alignment:NSTextAlignmentCenter];
    }
    return _timeLabel;
}
- (UIButton *)collectButton{
    if(_collectButton==nil){
        _collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_collectButton setImage:[UIImage imageNamed:@"wsc_b"] forState:UIControlStateNormal];
        [_collectButton addTarget:self action:@selector(collectButtonTap) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collectButton;
}
-(UILabel *)typeLabel{
    if(_typeLabel==nil){
        _typeLabel = [UILabel rb_labelWithTitle:@"进行中" color:RBColor(255, 255, 255) font:RBPingFangFont(12) alignment:NSTextAlignmentCenter];
    }
    return _typeLabel;
}
-(UILabel *)scoreVSLabel{
    if(_scoreVSLabel==nil){
        _scoreVSLabel = [UILabel rb_labelWithTitle:@"VS" color:RBColor(255, 255, 255) font:RBPingFangMediumFont(30) alignment:NSTextAlignmentCenter];
    }
    return _scoreVSLabel;
}
-(UILabel *)statueLabel{
    if(_statueLabel==nil){
        _statueLabel = [UILabel rb_labelWithTitle:@"（状态）" color:RBColor(255, 255, 255) font:RBPingFangFont(12) alignment:NSTextAlignmentCenter];
        _statueLabel.hidden = NO;
    }
    return _statueLabel;
}
-(UILabel *)homeTeamLabel{
    if(_homeTeamLabel==nil){
        _homeTeamLabel = [UILabel rb_labelWithTitle:@"（主）" color:RBColor(255, 255, 255) font:RBPingFangFont(12) alignment:NSTextAlignmentCenter];
    }
    return _homeTeamLabel;
}
- (UIImageView *)homeTeamImageView{
    if(_homeTeamImageView==nil){
        _homeTeamImageView = [[UIImageView alloc] init];
        _homeTeamImageView.image = [UIImage imageNamed:@""];
        _homeTeamImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _homeTeamImageView;
}
-(UILabel *)homeNameLabel{
    if(_homeNameLabel==nil){
        _homeNameLabel = [UILabel rb_labelWithTitle:@"主队 " color:RBColor(255, 255, 255) font:RBPingFangFont(12) alignment:NSTextAlignmentCenter];
    }
    return _homeNameLabel;
}
-(UILabel *)awayTeamLabel{
    if(_awayTeamLabel==nil){
        _awayTeamLabel = [UILabel rb_labelWithTitle:@"（客）" color:RBColor(255, 255, 255) font:RBPingFangFont(12) alignment:NSTextAlignmentCenter];
    }
    return _awayTeamLabel;
}
- (UIImageView *)awayTeamImageView{
    if(_awayTeamImageView==nil){
        _awayTeamImageView = [[UIImageView alloc] init];
        _awayTeamImageView.image = [UIImage imageNamed:@""];
        _awayTeamImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _awayTeamImageView;
}
-(UILabel *)awayNameLabel{
    if(_awayNameLabel==nil){
        _awayNameLabel = [UILabel rb_labelWithTitle:@"客队" color:RBColor(255, 255, 255) font:RBPingFangFont(12) alignment:NSTextAlignmentCenter];
    }
    return _awayNameLabel;
}
@end
